package com.example.easyscript.service.task;

import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.easyscript.bean.task.StepBean;
import com.example.easyscript.entity.task.Step;
import com.example.easyscript.enums.EnumUtils;
import com.example.easyscript.enums.StepTypeEnum;
import com.example.easyscript.mapper.StepMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author huzixian
 * @date 2023-03-08
 */
@Slf4j
@Service
public class StepService extends ServiceImpl<StepMapper, Step> {

    @Resource
    private ImageService imageService;

    public Map<Integer, StepBean> getStepMapByOperationId(Integer operationId) {
        List<Step> stepList = baseMapper.getStepListByOperationId(operationId);
        Map<Integer, StepBean> map = stepList.stream().map(item -> {
            StepBean stepBean = CglibUtil.copy(item, StepBean.class);
            String url = imageService.getUrlByImageId(stepBean.getImageId());
            stepBean.setPictureUrl(url);
            stepBean.setStepType(EnumUtils.getEnumByCode(item.getStepType(),StepTypeEnum.class));
            return stepBean;
        }).collect(Collectors.toMap(StepBean::getOrderId, item -> item));
        return map;
    }
}
